package meijuli.com.join51.business.ui.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.adapter.CustomAdapter;
import meijuli.com.join51.business.adapter.CustomHolder;
import meijuli.com.join51.business.model.Commodity;
import meijuli.com.join51.business.model.Order;

import java.util.ArrayList;
import java.util.List;

/**
 * 支持伸展的recyclerview
 *
 * @author jack 2017-03-29 09:41
 */
public class ScaleView extends FrameLayout {
    private RecyclerView scaleRecyclerView;
    private FrameLayout scaleFrameLayout;
    private TextView scaleTextView;
    private List<Commodity> orderList;
    private List<Commodity> orders;
    private Context mContext;
    private CustomAdapter<Commodity> mAdapter;
    /**
     * 当前是否展开状态
     */
    private boolean isSpread = false;

    public ScaleView(Context context) {
        this(context, null);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(scaleFrameLayout.getVisibility() == GONE) {
            return true;
        }
        int viewTop    = scaleFrameLayout.getTop();
        int viewLeft   = scaleFrameLayout.getLeft();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                double downX = ev.getX();
                double downY = ev.getY();
                if(downX > viewLeft && downY > viewTop ) {
                    return false;
                } else {
                    return true;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }

    public ScaleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_scaleview, this);
        scaleRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        scaleFrameLayout  = (FrameLayout) view.findViewById(R.id.scale_fl);
        scaleTextView     = (TextView) view.findViewById(R.id.scale_text);
        scaleFrameLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(orderList == null || mAdapter == null) return;
                if(isSpread) {
                    isSpread = false;
                    orders.clear();
                    orders.add(orderList.get(0));
                } else {
                    isSpread = true;
                    orders.clear();
                    orders.addAll(orderList);
                }
                setScaleText(isSpread);
                mAdapter.updateView(orders);
            }
        });
        orders    = new ArrayList<>();
        mContext = context;
    }

    private void setScaleText(boolean isSpread) {
        if(!isSpread) {
            Drawable spreadDrawable = getResources().getDrawable(R.drawable.cs_icon_down_arrow);
            spreadDrawable.setBounds(0, 0, spreadDrawable.getMinimumWidth(), spreadDrawable.getMinimumHeight());
            scaleTextView.setText("展开剩余商品");
            scaleTextView.setCompoundDrawables(null, null, spreadDrawable, null);
        } else {
            Drawable spreadDrawable = getResources().getDrawable(R.drawable.cs_icon_up_arrow);
            spreadDrawable.setBounds(0, 0, spreadDrawable.getMinimumWidth(), spreadDrawable.getMinimumHeight());
            scaleTextView.setCompoundDrawables(null, null,spreadDrawable, null);
            scaleTextView.setText("收起");
        }
    }

    public void setData(final Order order) {
        if(order == null) {
            return;
        }
        //orderList = order.getOrderDetailsList();
        if(orderList != null && orderList.size() > 0) {
            orders.clear();
            orders.add(orderList.get(0));
            scaleRecyclerView.setVisibility(VISIBLE);
            if(orderList.size() == 1) {
                scaleFrameLayout.setVisibility(GONE);
            } else {
                scaleFrameLayout.setVisibility(VISIBLE);
            }
            mAdapter = new CustomAdapter<Commodity>(orders, getContext(), R.layout.item_order_commodity, null) {
                @Override
                public void onBindData(CustomHolder holder, int position, Commodity goods) {
                    View view = holder.findView(R.id.view_line);
                    holder.setText(R.id.text_name, goods.getGoodsName());
                    holder.setText(R.id.text_price, "¥" + goods.getPrice());
                    holder.setText(R.id.text_count, "x" + goods.getGoodsNumber());
                    Glide.with(mContext).load(goods.getMainPicture()).centerCrop()
                            .error(R.mipmap.img_default).into((ImageView) holder.findView(R.id.img_commodity));
                    if(position < orders.size() - 1) {
                       view.setVisibility(VISIBLE);
                    } else {
                        view.setVisibility(GONE);
                    }
                }
            };
            scaleRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            scaleRecyclerView.setAdapter(mAdapter);
            setScaleText(isSpread);
        } else {
            scaleFrameLayout.setVisibility(GONE);
            scaleRecyclerView.setVisibility(GONE);
        }
    }

}
